<template>
  <a-modal v-model="visiable" :width="1000" title="批量设置库存，-1表示无限库存" @cancel="close" @ok="ok">
    <a-table :columns="columns" :data-source="dataSource" :bordered="false" :pagination="false" :loading="loading">
      <template slot="limitBuySlot" slot-scope="text,record,index">
        <a-input-number v-model="record.limitBuy"/>
      </template>
    </a-table>
  </a-modal>
</template>

<script>
import {postAction} from "@api/manage";

export default {
  data() {
    return {
      visiable: false,
      dataSource: [],
      loading: false,
      columns: [
        // {
        //   title: '店铺名称',
        //   dataIndex: 'merchantId_dictText',
        //   width: '25%',
        // },
        {
          title: '菜品名称',
          dataIndex: 'tradeName',
        },
        {
          title: '已售数量',
          dataIndex: 'soldCount',
        },
        {
          title: '库存数量',
          dataIndex: 'limitBuy',
          scopedSlots: {customRender: 'limitBuySlot'},
        },
      ],
    }
  },
  methods: {
    show(data) {
      this.visiable = true
      this.dataSource = JSON.parse(JSON.stringify(data));
    },
    close() {
      this.visiable = false;
    },
    ok() {
      let flag = false;
      for (let i = 0; i < this.dataSource.length; i++) {
        let item = this.dataSource[i]
        let { merchantId_dictText, tradeName,limitBuy } = item;
        console.log('limitBuy',limitBuy)
        if (!limitBuy) {
          this.$message.warning(`${merchantId_dictText}的${tradeName}商品库存不能为空`)
          flag = true
          break
        }
        if (limitBuy < -1) {
          this.$message.warning(`${merchantId_dictText}的${tradeName}商品库存不能小于-1`)
          flag = true
          break
        }
        if (String(limitBuy).split('.').length != 1) {
          this.$message.warning(`${merchantId_dictText}的${tradeName}商品库存只能为整数`)
          flag = true
          break
        }
      }
      if (flag) {
        return
      }

      let that = this;
      that.loading = true;
      postAction('/hc/hcCommodity/editLimitBuyBatch', this.dataSource)
          .then((res) => {
            if (res.success) {
              that.$emit('ok');
              that.visiable = false;
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.loading = false;
          }
      );
    },
  },
}
</script>

<style>
</style>